import request from '@/config/axios'

// 订单类型
export enum OrderType {
  Online = 1,
  Manual = 2
}
export const type2View = {
  [OrderType.Online]: { type: '', str: '在线订单' },
  [OrderType.Manual]: { type: 'success', str: '人工订单' }
}

// 订单来源
export enum SkuSource {
  Tenant = 1,
  Platform = 2
}
export const source2View = {
  [SkuSource.Tenant]: { type: '', str: '租户' },
  [SkuSource.Platform]: { type: 'success', str: '平台' }
}

// 订单状态
export enum OrderStatus {
  Unpaid = 1,
  Paid = 2,
  Canceled = 3
}
export const status2View = {
  [OrderStatus.Unpaid]: { type: '', str: '待支付' },
  [OrderStatus.Paid]: { type: 'success', str: '已支付' },
  [OrderStatus.Canceled]: { type: 'danger', str: '已取消' }
}

// 取消类型
export enum CancelType {
  UnCanceled = 1,
  UserCanceled = 2,
  SystemCanceled = 3
}
export const cancelType2View = {
  [CancelType.UnCanceled]: { type: '', str: '未取消' },
  [CancelType.UserCanceled]: { type: 'success', str: '用户取消' },
  [CancelType.SystemCanceled]: { type: 'danger', str: '系统取消' }
}

export interface Order {
  id: number
  orderNo: string
  orderType: OrderType // 1-在线订单 2-人工订单
  skuId: string
  skuSource: SkuSource // 1-租户 2-平台
  amount: number
  memberId: string
  signMemberId: string
  status: OrderStatus // 1-待支付 2-已支付 3-已取消
  remark: string
  signUpInfo: string
  payTime: number
  cancelTime: number
  orderPrice: number
  payOrderId: string
  payChannelCode: string
  cancelType: CancelType // 1-未取消 2-用户取消 3-系统取消
  createTime: number
}

export type OrderVO = Pick<
  Order,
  | 'id'
  | 'orderNo'
  | 'orderType'
  | 'orderPrice'
  | 'skuId'
  | 'skuSource'
  | 'amount'
  | 'status'
  | 'payTime'
  | 'cancelTime'
  | 'cancelType'
  | 'memberId'
  | 'signMemberId'
  | 'createTime'
> & {
  skuName: string // 根据 skuId 联表取 train_product_sku.sku_name
  skuPicUrl: string // 根据 skuId 联表取 train_product_sku.pic_url
  memberNickname: string // 根据 memberId 联表取 member_user.nickname
  memberAvatar: string // 根据 memberId 联表取 member_user.avatar
  signMemberNickname: string // 根据 signMemberId 联表取 member_user.nickname
  signMemberAvatar: string // 根据 signMemberId 联表取 member_user.avatar
}

const baseURL = '/train/order'

// 查询交易订单列表
export const getOrderPage = async (params: PageParam): Promise<PageResult<OrderVO>> => {
  return await request.post({ url: `${baseURL}/page-order`, data: params })
}

// 查询交易订单详情
export const getOrder = async (id: number) => {
  return await request.get({ url: `${baseURL}/get?id=` + id })
}

// 新增交易订单
export const createOrder = async (data: OrderVO) => {
  return await request.post({ url: `${baseURL}/create`, data })
}

// 修改交易订单
export const updateOrder = async (data: OrderVO) => {
  return await request.put({ url: `${baseURL}/update`, data })
}

// 删除交易订单
export const deleteOrder = async (id: number) => {
  return await request.delete({ url: `${baseURL}/delete?id=` + id })
}

// 导出交易订单 Excel
export const exportOrder = async (params) => {
  return await request.download({ url: `${baseURL}/export-excel`, params })
}
